/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package slicktemplate;

import java.io.Serializable;
import java.util.ArrayList;
import org.lwjgl.input.Mouse;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.state.StateBasedGame;

/**
 *
 * @author student
 */
public class BasicBuilding extends Collideable implements Building, Renderable, Updateable,Serializable {

    private float x, y;
    SerializableImage sprite;
    public int state = 0;
    /* state
     * 0: Button
     * 1: Placer
     * 2: Placed
     */
    private boolean lastPressed = false;
    static ArrayList<BasicBuilding> all = new ArrayList<BasicBuilding>();
    private boolean[] mouseState = new boolean[3];

    public BasicBuilding(float x, float y) {
        this.x = MathHelper.align_to_grid_point(x);
        this.y = MathHelper.align_to_grid_point(y);
        all.add(this);
        try {
            sprite =  new SerializableImage("sprites/building.png");
        } catch (Exception e) {
            e.printStackTrace();
        }
        boundingBox = new Rectangle(x, y, sprite.getWidth() - 1, sprite.getHeight() - 1);
    }

    public float getX() {
        return x;
    }

    public void setX(float newx) {
        x = newx;
    }

    public void setY(float newy) {
        y = newy;
    }

    public float getY() {
        return y;
    }

    public void render(Graphics g) {
        sprite.draw(x, y);

    }

    public boolean update(Input input, StateBasedGame sb) {
        if (sb.getCurrentStateID() == Game.LEVELEDIT_STATE) {
            handleMouse(input, sb);
        }else{
            if(state != 2){
                return true;
            }
        }
        return false;
    }

    private void handleMouse(Input input, StateBasedGame game) {
        switch (state) {
            case 0:
                if ((input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON) == true && mouseState[Input.MOUSE_LEFT_BUTTON] == false) && getCollision(input.getMouseX(), input.getMouseY())) {
                    for (int i = 0; i < all.size(); i++) {
                        if (all.get(i).state == 1) {
                            return;
                        } else {
                            break;
                        }
                    }
                    BasicBuilding placer = new BasicBuilding(x, y);
                    placer.state = 1;
                    boolean[] temp = new boolean[3];
                    temp[0] = true;
                    temp[1] = true;
                    temp[2] = true;
                    placer.mouseState = temp.clone();
                }
                break;
            case 1:
                x = MathHelper.align_to_grid_point(input.getMouseX());
                y = MathHelper.align_to_grid_point(input.getMouseY());
                System.out.println(input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON) + "," + mouseState[Input.MOUSE_LEFT_BUTTON]);
                if ((input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON) == true && mouseState[Input.MOUSE_LEFT_BUTTON] == false)) {
                    if (!Collideable.getCollisionLists(new Rectangle(x, y, sprite.getWidth() - 1, sprite.getHeight() - 1), LevelEditor.placeOverList)) {
                        BasicBuilding placed = new BasicBuilding(x, y);
                        placed.state = 2;
                        if (input.isKeyDown(Input.KEY_LSHIFT) || input.isKeyDown(Input.KEY_RSHIFT)) {
                            return;
                        } else {
                            all.remove(this);
                        }
                    }
                }
                break;
            case 2:
                boundingBox = new Rectangle(x, y, sprite.getWidth() - 1, sprite.getHeight() - 1);
                break;
            default:
                System.out.println("Unknown State in BasicBuilding");
                System.exit(1);
                break;
        }
        mouseState[Input.MOUSE_LEFT_BUTTON] = input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON);
        mouseState[Input.MOUSE_MIDDLE_BUTTON] = input.isMouseButtonDown(Input.MOUSE_MIDDLE_BUTTON);
        mouseState[Input.MOUSE_RIGHT_BUTTON] = input.isMouseButtonDown(Input.MOUSE_RIGHT_BUTTON);
    }
}
